package com.cn.xiaonuo.sys.modular.news;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.xiaonuo.core.context.login.LoginContextHolder;
import com.cn.xiaonuo.core.factory.PageFactory;
import com.cn.xiaonuo.core.pojo.login.SysLoginUser;
import com.cn.xiaonuo.core.pojo.page.PageResult;
import com.cn.xiaonuo.core.pojo.response.ResponseData;
import com.cn.xiaonuo.core.pojo.response.SuccessResponseData;
import com.cn.xiaonuo.sys.modular.images.FileUrlUtls;
import com.cn.xiaonuo.sys.modular.news.entity.News;
import com.cn.xiaonuo.sys.modular.news.entity.Type;
import com.cn.xiaonuo.sys.modular.news.entity.User;
import com.cn.xiaonuo.sys.modular.news.mapper.NewsMapper;
import com.cn.xiaonuo.sys.modular.news.mapper.TypeMapper;
import com.cn.xiaonuo.sys.modular.news.mapper.UserMapper;
import com.cn.xiaonuo.sys.modular.news.vo.NewsVo;
import com.cn.xiaonuo.sys.modular.user.entity.SysUser;
import com.cn.xiaonuo.sys.modular.user.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/news/")
public class newsController {

    @Autowired
    private NewsMapper newsMapper;


    @GetMapping("/index")
    public String index(){
        return "business/news/index.html";
    }

    @GetMapping("/form")
    public String form() {
        return "business/news/form.html";
    }

    /**
     * 查看详情
     * @return
     */
    @GetMapping("/detailPage")
    public String detailPage() {
        return "business/news/detail.html";
    }

    /**
     * 查询新闻列表
     * @param newsVo
     * @return
     */
    @GetMapping("page")
    @ResponseBody
    public PageResult<News> findNewsByPage(NewsVo newsVo){
        QueryWrapper<News> queryWrapper=new QueryWrapper<>();
        if (!ObjectUtil.isNotNull(newsVo)){
            if (!StringUtils.isEmpty(newsVo.getTitle())){
                queryWrapper.like("title",newsVo.getTitle());
            }
            if (newsVo.getType()!=null){
                queryWrapper.eq("type",newsVo.getType());
            }
        }
        Page<News> newsPage = newsMapper.selectPage(PageFactory.defaultPage(), queryWrapper);
        return new PageResult<>(newsPage);
    }

    /**
     * 新增新闻
     * @param news
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public ResponseData addNews(@RequestBody News news){
        log.info("新增新闻参数=="+news);
        SysLoginUser sysLoginUser = LoginContextHolder.me().getSysLoginUser();
        news.setUsername(sysLoginUser.getUsername());
        news.setUserid(sysLoginUser.getId());
        news.setCreateTime(new Date());
        newsMapper.insert(news);
        return new SuccessResponseData();
    }

    /**
     * 编辑新闻表单提交
     * @param news
     * @return
     */
    @PostMapping("edit")
    @ResponseBody
    public ResponseData editNews(@RequestBody News news){
        newsMapper.updateById(news);
        return new SuccessResponseData();
    }

    /**
     * 删除新闻
     * @param newsList
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public ResponseData deleteNews(@RequestBody List<News> newsList){
        for (News news : newsList) {
            newsMapper.deleteById(news.getId());
        }
        return new SuccessResponseData();
    }

    /**
     * 修改 发布、待发布状态
     * @param news
     * @return
     */
    @PostMapping("changeStatus")
    @ResponseBody
    public ResponseData changeStatus(@RequestBody News news){
        if (news==null){
            return ResponseData.error("参数异常");
        }
        News news1 = new News();
        news1.setId(news.getId());
        news1.setStatus(news.getStatus());
        newsMapper.updateById(news1);
        return ResponseData.success();
    }

    /**
     * 查询新闻详情
     * @param news
     * @return
     */
    @GetMapping("detail")
    @ResponseBody
    public ResponseData detail( News news){
        News news1 = newsMapper.selectById(news.getId());
        return new SuccessResponseData(news1);
    }


}
